//
//  DengLuViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/9/8.
//  Copyright © 2016年 ping. All rights reserved.
//登录界面

#import "DengLuViewController.h"
#import "ZhuCeViewController.h"
#import "WangJiViewController.h"
#import "XiuGaiViewController.h"
@interface DengLuViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UIButton *dengLu;
@property (weak, nonatomic) IBOutlet UITextField *miMa;
@property (weak, nonatomic) IBOutlet UIView *backView;
- (IBAction)wangJi:(id)sender;

- (IBAction)zhuCe:(id)sender;

- (IBAction)dingLu:(id)sender;

@end

@implementation DengLuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView * backImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backImage.image = [UIImage imageNamed:@"aaaa"];
    [self.view addSubview:backImage];
    UIView * view = [[[NSBundle mainBundle]loadNibNamed:@"DengLu" owner:self options:nil] lastObject];
    view.frame = CGRectMake(0, 150*_kscH, KScreenWidth, 200);
    [self.view addSubview:view];
    //关闭
    UIButton * guanBi = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-30, 24, 20, 20)];
    [guanBi setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [guanBi addTarget:self action:@selector(guanBi) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:guanBi];

    _dengLu.layer.cornerRadius = 4;
    _backView.layer.borderWidth = 2;
    _backView.layer.borderColor = [UIColor whiteColor].CGColor;
    _backView.layer.cornerRadius = 4;
    _backView.layer.masksToBounds =YES;
      [_name setValue:[UIColor whiteColor]forKeyPath:@"_placeholderLabel.textColor"];
      [_miMa setValue:[UIColor whiteColor]forKeyPath:@"_placeholderLabel.textColor"];
    [self foot];
    
    }
-(void)guanBi{
    [self.navigationController popViewControllerAnimated:YES];
    if (_a == 1) {
     [[NSNotificationCenter defaultCenter]postNotificationName:@"未登录" object:nil];
    }
}

-(void)foot{
    UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake((KScreenWidth-150)/2, KScreenHeight-35, 150, 15)];
    lable.font = [UIFont systemFontOfSize:11];
    lable.textColor = [UIColor whiteColor];
    NSMutableAttributedString * abs = [[NSMutableAttributedString alloc]initWithString:@"点击注册即同意商城用户协议"];
    [abs addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(7, 6)];
    lable.attributedText = abs;
    UIImageView * footImage = [[UIImageView alloc]initWithFrame:CGRectMake(lable.left-18, KScreenHeight-35, 15, 15)];
    footImage.image = [UIImage imageNamed:@"√-1"];
    [self.view addSubview:lable];
    [self.view addSubview:footImage];
}

#pragma mark 点击键盘return
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if([textField isEqual:_name]){
        [_miMa becomeFirstResponder];
    }else if([textField isEqual:_miMa]){
        [textField endEditing:YES];
     }
    return YES;
}
#pragma mark 忘记密码
- (IBAction)wangJi:(id)sender {
    WangJiViewController * wang = [[WangJiViewController alloc]init];
    [self.navigationController pushViewController:wang animated:YES];
}
#pragma mark 注册
- (IBAction)zhuCe:(id)sender {
    ZhuCeViewController * zhu = [[ZhuCeViewController alloc]init];
    [self.navigationController pushViewController:zhu animated:YES];
}
#pragma mark 登录
- (IBAction)dingLu:(id)sender {
    if (_name.text.length == 0 || _miMa.text.length == 0) {
        return;
    }
    NSMutableDictionary * dic =[[NSMutableDictionary alloc]init];
    [dic setObject:_name.text forKey:@"userName"];
    [dic setObject:_miMa.text forKey:@"password"];
    [NetWorking NEWjieKouQingQiu:DL_dengLuMain ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                [TCUtils showAlertViewWithTitle:@"提示" message:@"登录成功" delegate:self cancel:@"确定" other:nil tag:0];
                [self defaultSaveUserName:dic[Response][@"account"][@"username"]];
                [self defaultSaveUserPhone:dic[Response][@"phone"]];

                [self defaultSaveUserId:dic[Response][@"_id"]];

                [self defaultSaveUserPassWord:_miMa.text];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"用户名" object:nil];
                 [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }];
}
@end
